/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.control;

import it.polimi.se2.meteocal.entity.Event;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
/**
 *
 * @author Ugo
 */
@Stateless
public class EventManager {
    
    @PersistenceContext
    public EntityManager em;
    
    public void create(Event entity) {
        em.persist(entity);
    }

    public void edit(Event entity) {
        em.merge(entity);
    }

    public void remove(Event entity) {
        em.remove(em.merge(entity));
    }
    
    public Event getEventById(Long id) {
        try{
        return em.createNamedQuery(Event.findById, Event.class)
                .setParameter("id", id)
                .getSingleResult(); 
        }
        catch(NoResultException e){
            return null;
        }
    }
    
    public List<Event> getAllEvent() {
        return em.createNamedQuery(Event.findAll, Event.class)
                .getResultList();
    }

}